/**************************************************************
 *@ProjectName: blweibo
 *@FileName: UserInfoActivity2.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiweibo.models.StaticInfo;
import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class UserInfoActivity2.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class UserInfoActivity2 extends BaseActivity {

    /** The iv portrait. */
    private ImageView ivPortrait;

    /** The tv nick. */
    private TextView tvNick;

    /** The iv gental. */
    private ImageView ivGental;

    /** The btn reload. */
    private Button btnReload;

    /** The btn at. */
    private Button btnAt;

    /** The btn message. */
    private Button btnMessage;

    /** The tv address1_content. */
    private TextView tvAddress1_content;

    /** The tv account_introduce_content. */
    private TextView tvAccount_introduce_content;

    /** The ll attention. */
    private RelativeLayout llAttention;

    /** The tv attention_count. */
    private TextView tvAttention_count;

    /** The rl weibo. */
    private LinearLayout rlWeibo;

    /** The tv weibo_count. */
    private TextView tvWeibo_count;

    /** The ll fans. */
    private LinearLayout llFans;

    /** The tv fans_count. */
    private TextView tvFans_count;

    /** The rl fav. */
    private LinearLayout rlFav;

    /** The tv fav_ count. */
    private TextView tvFav_Count;

    /** The str uid. */
    private String strUid;

    /** The m user. */
    private User mUser;

    /** The btn friend. */
    private Button btnFriend;

    /** The iv v. */
    private ImageView ivV;

    /** The iv hr. */
    private ImageView ivHR;

    /** The iv li. */
    private ImageView ivLi;

    /** The iv qi. */
    private ImageView ivQi;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.user_info);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.title_other_user_info),
                getString(R.string.main_home));
        mUser = (User) getIntent().getSerializableExtra("user");
        strUid = mUser.getUid();
        ivPortrait = (ImageView) findViewById(R.id.ivPortrait);
        tvNick = (TextView) findViewById(R.id.tvNick);
        ivGental = (ImageView) findViewById(R.id.ivGental);
        btnReload = (Button) findViewById(R.id.btnReload);
        btnAt = (Button) findViewById(R.id.btnAt);
        btnMessage = (Button) findViewById(R.id.btnMessage);
        btnReload.setOnClickListener(this);
        btnAt.setOnClickListener(this);
        btnMessage.setOnClickListener(this);
        tvAddress1_content = (TextView) findViewById(R.id.tvAddress1_content);
        tvAccount_introduce_content = (TextView) findViewById(R.id.tvAccount_introduce_content);
        llAttention = (RelativeLayout) findViewById(R.id.llAttention);
        tvAttention_count = (TextView) findViewById(R.id.tvAttention_count);
        rlWeibo = (LinearLayout) findViewById(R.id.rlWeibo);
        tvWeibo_count = (TextView) findViewById(R.id.tvWeibo_count);
        llFans = (LinearLayout) findViewById(R.id.llFans);
        tvFans_count = (TextView) findViewById(R.id.tvFans_count);
        rlFav = (LinearLayout) findViewById(R.id.rlFav);
        tvFav_Count = (TextView) findViewById(R.id.tvFav_Count);
        btnFriend = (Button) findViewById(R.id.bnEdit);
        if (strUid.equals(StaticInfo.mUid)) {
            btnFriend.setVisibility(View.INVISIBLE);
        } else {
            btnFriend.setVisibility(View.VISIBLE);
        }
        btnFriend.setOnClickListener(this);
        rlWeibo.setOnClickListener(this);
        if (!strUid.equals("1") && !strUid.equals("12")) {
            llAttention.setOnClickListener(this);
            llFans.setOnClickListener(this);
            rlFav.setOnClickListener(this);
        }
        ivV = (ImageView) findViewById(R.id.ivItemV);
        ivHR = (ImageView) findViewById(R.id.ivItemHR);
        ivLi = (ImageView) findViewById(R.id.ivItemLi);
        ivQi = (ImageView) findViewById(R.id.ivItemQi);
        setUI();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onClick(android.view.View)
     */
    @Override
    public void onClick(View paramView) {
        if (paramView == btnReload) {
            new InfoTask().execute(0);
        } else if (paramView == btnAt) {
            startActivity(new Intent(this, EditActivity.class).putExtra("mode", 0).putExtra("content",
                    "@" + mUser.getUsername() + ":"));
        } else if (paramView == btnMessage) {
            startActivity(new Intent(this, EditActivity.class).putExtra("mode", 3).putExtra("uid", mUser.getUid()));
        } else if (paramView == llAttention) {
            startActivity(new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 2).putExtra("uid",
                    mUser.getUid()));
        } else if (paramView == rlWeibo) {
            startActivity(new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 1).putExtra("uid",
                    mUser.getUid()));
        } else if (paramView == llFans) {
            startActivity(new Intent(this, UserWeiboAttentionFansList.class).putExtra("mode", 3).putExtra("uid",
                    mUser.getUid()));
        } else if (paramView == btnFriend) {
            new AttentionTask().execute(0);
        }
        super.onClick(paramView);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        case 0:
            startActivity(new Intent(this, MainTabActivity.class).setAction("com.sina.weibo.action.go_back_to_home")
                    .putExtra("MODE_KEY", 0));
            finish();
            break;
        case 1:
            finish();
            break;
        }
    }

    /**
     * Sets the ui.
     */
    private void setUI() {
        new FaceTask(ivPortrait, mUser.getFace()).execute(0);
        User user = mUser;
        if (user.getV()) {
            ivV.setVisibility(View.VISIBLE);
        }
        if (user.getHR()) {
            ivHR.setVisibility(View.VISIBLE);
        }
        if (user.getL()) {
            ivLi.setVisibility(View.VISIBLE);
        }
        if (user.getQ()) {
            ivQi.setVisibility(View.VISIBLE);
        }

        if (!strUid.equals("1") && !strUid.equals("12")) {
            tvFav_Count.setText(mUser.getFavourite_count());
            tvFans_count.setText(mUser.getFans_count());
            tvAttention_count.setText(mUser.getFollow_count());
        } else {
            String strHide = "隐藏";
            tvFav_Count.setText(strHide);
            tvFans_count.setText(strHide);
            tvAttention_count.setText(strHide);
        }
        tvWeibo_count.setText(mUser.getTopic_count());
        tvNick.setText(mUser.getUsername());
        tvAccount_introduce_content.setText(mUser.getAboutme());
        tvAddress1_content.setText(mUser.getCity());
        ivGental.setImageResource(mUser.getGender().equals("1") ? R.drawable.icon_male : R.drawable.icon_female);
        new FriendTask().execute(0);
    }

    /**
     * The Class InfoTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class InfoTask extends AsyncTask<Object, Integer, User> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected User doInBackground(Object... paramArrayOfString) {
            int id = strUid != null && strUid.length() > 0 ? Integer.parseInt(strUid) : 0;
            String strUrl;
            if (id == 0) {
                strUrl = Constant.USERS_SHOW;
            } else {
                strUrl = Constant.USERS_SHOW + "?uid=" + id;
            }
            mUser = HttpUtil.getUserInfo(UserInfoActivity2.this, strUrl);
            return mUser;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(User result) {
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(UserInfoActivity2.this);
            pd.setMessage("请稍候……");
            pd.show();
        }

    }

    /**
     * The Class FriendTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FriendTask extends AsyncTask<Object, Integer, JSONObject> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected JSONObject doInBackground(Object... paramArrayOfString) {
            return HttpUtil.isFriend(UserInfoActivity2.this, mUser.getUid());
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(JSONObject result) {
            if (result == null || result.isNull("source")) {
                finish();
                return;
            }
            boolean b1 = result.optJSONObject("source").optBoolean("following");
            boolean b2 = result.optJSONObject("source").optBoolean("followed_by");
            if (!b1) {
                if (mUser.getGender().equals("1")) {
                    btnFriend.setText(R.string.attend_to_him);
                } else {
                    btnFriend.setText(R.string.attend_to_her);
                }
                btnFriend.setBackgroundResource(R.drawable.userinfo_add_attention);
            } else {
                btnFriend.setText(R.string.user_delattention);
                btnFriend.setBackgroundResource(R.drawable.userinfo_cancel_attention);
            }
            if (b1 && b2) {
                btnFriend.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_2attention, 0, 0, 0);
                int j = getResources().getDimensionPixelSize(R.dimen.mBtnEditOrAttend_padding_left);
                btnFriend.setPadding(j, 0, 0, 0);
            } else {
                btnFriend.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                btnFriend.setPadding(0, 0, 0, 0);
            }
        }
    }

    /**
     * The Class FaceTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FaceTask extends AsyncTask<Object, Integer, Bitmap> {

        /** The piv. */
        private ImageView piv;

        /** The str url. */
        private String strUrl;

        /**
         * Instantiates a new face task.
         * 
         * @param iv
         *            the iv
         * @param url
         *            the url
         */
        public FaceTask(ImageView iv, String url) {
            piv = iv;
            strUrl = url;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Bitmap doInBackground(Object... params) {
            return HttpUtil.getImage(UserInfoActivity2.this, strUrl);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Bitmap result) {
            piv.setImageBitmap(result);
        }
    }

    /**
     * The Class AttentionTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class AttentionTask extends AsyncTask<Object, Integer, Object> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            if (btnFriend.getText().toString().equals(getString(R.string.user_delattention))) {
                mUser = HttpUtil.getUserInfo(UserInfoActivity2.this, Constant.FRIENDSHIPS_DESTROY + mUser.getUid()
                        + ".json");
            } else {
                mUser = HttpUtil.getUserInfo(UserInfoActivity2.this, Constant.CREATE + mUser.getUid() + ".json");
            }
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            setUI();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(UserInfoActivity2.this);
            pd.setMessage("请稍候……");
            pd.show();
        }

    }
}
